package com.sanpu.kxe.business.main.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.ActHomePopOutput;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.banner.MZBannerView;
import com.sanpu.kxe.module.banner.holder.MZHolderCreator;
import com.sanpu.kxe.module.banner.holder.MZViewHolder;
import com.sanpu.kxe.module.roundview.RoundedImageView;
import com.sanpu.kxe.rxservice.RequestMap;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：首页弹框
 * Created by 薛勇军 on 2018/7/9.
 */

public class ActHomePopupView extends Dialog {

    @Bind(R.id.banner)
    MZBannerView vMZBannerView;
    @Bind(R.id.closeIv)
    ImageView closeIv;

    public ActHomePopupView(Context context, List<ActHomePopOutput> retDatBean) {
        super(context, R.style.dialog_normal);
        setContentView(R.layout.popup_act_home);
        ButterKnife.bind(this);
        setCancelable(false);
        showBanner(retDatBean);
    }

    @OnClick({R.id.moreEvent})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moreEvent:
                MainOpenProvider.getInstance().openEventCenterActivity(getContext());
                dismiss();
                break;
        }
    }

    public void setCancelClick(View.OnClickListener clickListener) {
        closeIv.setOnClickListener(clickListener);
    }

    private void showBanner(List<ActHomePopOutput> retDatBean) {
        vMZBannerView.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
                try {
                    ActHomePopOutput output = (ActHomePopOutput) vMZBannerView.getAdapter().getItem(position);
                    if (output != null && output.getActPath() != null) {
                        ActHomePopOutput.ActPath actPath = output.getActPath();
                        if (actPath != null && !TextUtils.isEmpty(actPath.getType())) {
                            switch (actPath.getType()) {
                                case "1":
                                    String appNative = actPath.getAndroid();
                                    ARouter.getInstance()
                                            .build(appNative)
                                            .withTransition(R.anim.in_from_right, R.anim.in_to_left)
                                            .navigation();
                                    ARouter.getInstance().build(appNative);
                                    dismiss();
                                    break;
                                case "2":
                                    String url = actPath.getUrl() + RequestMap.sessionIdReqStr();
                                    MainOpenProvider.getInstance().openBrowserX5Activity(getContext(), url, true);
                                    dismiss();
                                    break;
                                case "3":
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        vMZBannerView.setPages(retDatBean, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        if (retDatBean.size() > 1)
            vMZBannerView.start();
    }

    public static class BannerViewHolder implements MZViewHolder<ActHomePopOutput> {
        private RoundedImageView mImageView;

        @Override
        public View createView(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_act_home_popup, null);
            mImageView = view.findViewById(R.id.item_event_image);
            return view;
        }

        @Override
        public void onBind(Context context, int i, ActHomePopOutput image) {
            JKImageLoader.getInstance().displayImage(mImageView.getContext(),
                    UrlConstants.getBaseUrl() + image.getAddPic(), mImageView, R.drawable.shape_placeholder);
        }
    }
}
